//
//  SYDRestPayMentWordController.m
//  PinGJByOwnerSide
//
//  Created by lantaiyuan on 2017/8/12.
//  Copyright © 2017年 lantaiyuan. All rights reserved.
//

#import "SYDRestPayMentWordController.h"
#import "SYDRetrievePayPasswordController.h"
@interface SYDRestPayMentWordController ()<UITextFieldDelegate,PassWordViewDelegate>
@property(nonatomic,strong)UILabel *titleLabel;
@property (nonatomic, strong) SYPasswordView *pasView;
@property(nonatomic,strong)UIButton *sureBtn;
@property(nonatomic,strong)UILabel *showLabel;
@property(nonatomic,assign)NSInteger settingCount;

@end

@implementation SYDRestPayMentWordController

#pragma mark - 导航栏
- (void)navigationBarHandle{
    
    WEAKSELF
    [weakSelf backButtonAction:^{
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }];
    
    [weakSelf rightNavButtonWithTitle:@"找回密码" action:^{
        SYDRetrievePayPasswordController *payVc = [[SYDRetrievePayPasswordController alloc]init];
        [weakSelf.navigationController pushViewController:payVc animated:YES];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.navigationBar.title = @"重置支付密码";
    [self navigationBarHandle];
    [self initResetPayMentWord];

}
-(void)initResetPayMentWord{
    self.settingCount = 0;
    self.titleLabel = [[UILabel alloc]init];
    [self.view addSubview:self.titleLabel];
    
    self.titleLabel.text = @"请输入旧的支付密码，以验证身份";
    self.titleLabel.textColor = SUB_TITLE_COLOR;
    self.titleLabel.font = [UIFont systemFontOfSize:14];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.numberOfLines = 0;
    
    //字符密码
    self.pasView = [[SYPasswordView alloc] initWithFrame:CGRectMake(15, 133, SCREEN_WIDTH - 30, 44)];
    self.pasView.delegate = self;
    [self.view addSubview:self.pasView];
    
    //确认按钮
    self.sureBtn = [[UIButton alloc]init];
    self.sureBtn.backgroundColor = GREEN_COLOR;
    [self.sureBtn drawBorderWithColor:GREEN_COLOR radius:5];
    [self.sureBtn setTitle:@"完成" forState:UIControlStateNormal];
    [self.sureBtn setTitleColor:WHITE_COLOR forState:UIControlStateNormal];
#pragma mark---约束
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(14));
        make.centerX.equalTo(self.view.mas_centerX);
        make.top.equalTo(self.view.mas_top).offset(99);
        make.left.equalTo(self.view.mas_left).offset(15);
        make.right.equalTo(self.view.mas_right).offset(-15);
    }];
}
-(void)showLabelUI{
    
    [UIView animateWithDuration:2.0 animations:^{
        self.showLabel = [[UILabel alloc]init];
        [self.view addSubview:self.showLabel];
        self.showLabel.backgroundColor  = [UIColor colorWithHexString:@"#03111e"];
        self.showLabel.alpha  = 0.5;
        [self.showLabel drawBorderWithColor:[UIColor colorWithHexString:@"#03111e"] radius:4];
        self.showLabel.layer.cornerRadius = 4;
        self.showLabel.layer.masksToBounds = YES;
        self.showLabel.text = @"修改密码成功";
        self.showLabel.textColor  = WHITE_COLOR;
        self.showLabel.font = [UIFont systemFontOfSize:14];
        self.showLabel.textAlignment = NSTextAlignmentCenter;
        [self.showLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@(33));
            make.bottom.equalTo(self.view.mas_bottom).offset(-265);
            make.width.equalTo(@(120));
            make.centerX.equalTo(self.view.mas_centerX);
        }];
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:2.0 animations:^{
            [self.showLabel removeFromSuperview];
            [self.navigationController popViewControllerAnimated:YES];
        }];
    }];
}
#pragma mark---PassWordViewDelegate
-(void)passwordTextFieldChanged:(NSString *)textfieldValue{
    
    
    NSLog(@"----------%ld----%@ ",self.settingCount,self.userAccountModel.payCode);
    if (self.settingCount == 0) {
        if ([[NSString sha1:textfieldValue] isEqualToString:self.userAccountModel.payCode]) {
            [self.pasView clearUpPassword];
            dispatch_async(dispatch_get_main_queue(), ^{
                self.titleLabel.text = @"请设置支付密码,建议勿与银行卡取款密码相同";
                self.settingCount ++;
            });
        }else{
            
            [SVProgressHUD displayInfoWithStatus:@"旧的支付密码不对,请重新输入"];
            [self.pasView clearUpPassword];
        }
    }else if (self.settingCount == 1){
        [YMSaveTool setObject:textfieldValue forKey:@"paymentWord"];
       
        [self.pasView clearUpPassword];
        dispatch_async(dispatch_get_main_queue(), ^{
            self.titleLabel.text = @"请再次输入以确认";
            self.settingCount ++;
        });
        
    }else if (self.settingCount == 2){
        [YMSaveTool setObject:textfieldValue forKey:@"confirmPassword"];
        if ([[YMSaveTool valueForKey:@"confirmPassword"] isEqualToString:[YMSaveTool valueForKey:@"paymentWord"]]) {
            
            [[SYDNetwork shareTool]setPayCodeAccountWithCityCode:[YMSaveTool valueForKey:CityCode] payCode:[YMSaveTool valueForKey:@"confirmPassword"] userId:[YMSaveTool valueForKey:USER_ID] success:^(NSURLSessionDataTask *task, id responseObject) {
                @try {
                    NSLog(@"responseObject:%@",responseObject);
                    if ([responseObject[@"statusCode"]integerValue] == 200) {
                        dispatch_async(dispatch_get_main_queue(), ^{
                            [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
                            [self.view addSubview:self.sureBtn];
                            [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                                make.height.equalTo(@(40));
                                make.top.equalTo(self.pasView.mas_bottom).offset(50);
                                make.left.equalTo(self.view.mas_left).offset(15);
                                make.right.equalTo(self.view.mas_right).offset(-15);
                                
                            }];
                            [[self.sureBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
                                NSLog(@"点击了确认按钮");
                                [self showLabelUI];
                            }];
                        });
                        self.settingCount = 0;
                    }else{
                        [SVProgressHUD displayInfoWithStatus:responseObject[@"msg"]];
                    }
                } @catch (NSException *exception) {
                    NSLog(@"exception:%@",exception.description);
                } @finally {
                }
            } failure:^(NSURLSessionDataTask *task, NSError *error) {
                NSLog(@"error:%@",error);
            }];
            
        }else{
            [SVProgressHUD displayInfoWithStatus:@"两次输入的密码不符" time:1.5f];
            [self.pasView clearUpPassword];
        }
    }
    
}
//将通知中心移除

- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
